﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace CPA_Monitor
{
    public class Networks
    {
        public class CPANetwork
        {
            public string Username { get; set; }
            public string Password { get; set; }
            public string Company { get; set; }
            public string LoginUrl { get; set; }
            public string StatsUrl { get; set; }
            public string RegexProfit { get; set; }
            public string RegexClicks { get; set; }
            public string RegexEPC { get; set; }
            public string LoginPostDataKeys { get; set; }
            public string LoginPostDataValues { get; set; }
            public string StatsPostDataKeys { get; set; }
            public string StatsPostDataValues { get; set; }
            public string LoggedInIndicator { get; set; }
            public bool Enabled { get; set; }
            public NetworkStats Stats = new NetworkStats();

            public Communications.PostData GetPostDataLogin()
            {
                return FormPostData(LoginPostDataKeys, LoginPostDataValues);
            }

            public Communications.PostData GetPostDataStats()
            {
                return FormPostData(StatsPostDataKeys, StatsPostDataValues);
            }

            Communications.PostData FormPostData(string keys, string values)
            {
                var postData = new Communications.PostData();
                var postKeys = Regex.Split(keys, "\n");
                var postValues = Regex.Split(values, "\n");
                for (int i = 0; i < postKeys.Length; i++)
                {
                    postData.AddValue(postKeys[i], postValues[i].Replace("{USERNAME}", Username).Replace("{PASSWORD}", Password));
                }
                return postData;
            }
        }

        public class NetworkStats
        {
            [NonSerialized()] public string Profit = "Profit";
            [NonSerialized()] public string Clicks = "Clicks";
            [NonSerialized()] public string EPC = "EPC";
        }

        public static List<CPANetwork> NetworkEntries = new List<CPANetwork>();

        public static void Load()
        {
            NetworkEntries.Clear();
            if (Directory.Exists("networks"))
            {
                foreach (string networkFile in Directory.GetFiles("networks"))
                {
                    var network = new CPANetwork();
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(network.GetType());
                    using (var stream = new FileStream(networkFile, FileMode.OpenOrCreate))
                    {
                        network = x.Deserialize(stream) as CPANetwork;
                        NetworkEntries.Add(network);
                    }
                }
            }
        }

        public static void Save()
        {
            new DirectoryInfo("networks").GetFiles().ToList().ForEach(x => x.Delete());

            Directory.CreateDirectory("networks");
            foreach (CPANetwork network in NetworkEntries)
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(network.GetType());
                using (var stream = new FileStream("networks\\" + network.Company + ".xml", FileMode.Create))
                {
                    x.Serialize(stream, network);
                }
            }
        }

    }
}
